package com.exercises;


import java.util.*;

class Exercise49 {
    public static void main(String[] args) {
        String a = "bac";
        char[] arr = a.toCharArray();
        Arrays.sort(arr);

        String b = "cba";
        char[] brr = b.toCharArray();
        Arrays.sort(brr);
        System.out.println(String.valueOf(arr).hashCode());
        System.out.println(String.valueOf(brr).hashCode());
    }


    public List<List<String>> groupAnagrams(String[] strs) {
        Map<String, List<String>> map = new HashMap<>();

        for (String str : strs) {
            char[] arr = str.toCharArray();
            Arrays.sort(arr);
            String union = String.valueOf(arr);
            if (map.containsKey(union)) {
                map.get(union).add(str);
            } else {
                List<String> item = new ArrayList<>();
                item.add(str);
                map.put(union, item);
            }
        }

        List<List<String>> result = new ArrayList<>();
        map.forEach((k, v) -> {
            result.add(v);
        });
        return result;
    }

}

